import { isArray } from "./verify";
export interface rectSize  extends UniApp.NodeInfo {}

function _getRect(
	ctx: any,
	selector?: string | any,
): Promise<UniApp.NodeInfo >
function _getRect(
	ctx: any,
	selector?: string | any,
	all?:boolean
): Promise<Array<UniApp.NodeInfo>>
function _getRect(
	ctx: any,
	selector?: string | any,
	all?: boolean
): Promise<Array<UniApp.NodeInfo>|UniApp.NodeInfo > {
	return new Promise((resolve) => {
		uni
			.createSelectorQuery()
			.in(ctx)
		[all ? "selectAll" : "select"](selector)
			.boundingClientRect(
				(rect) => {
					if (all && isArray(rect) && rect.length) {
						resolve(rect);
					}
					if (!all && rect) {
						resolve(rect);
					}
				}
			)
			.exec();

	});
}
export const getRect = _getRect
// #ifdef APP-NVUE
let dom:any

// #endif

export function getNvueRect(
	selector: any
): Promise<rectSize> {
	// #ifdef APP-NVUE
	if(!dom){
		dom = uni.requireNativePlugin('dom')
	}
	
	return new Promise((resolve) => {
		// nvue下，使用dom模块查询元素高度
		dom.getComponentRect(selector, (res: { size: rectSize }) => {
			resolve(res.size)
		})
	});
	// #endif
}

